package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import model.Notes;
import util.HibernateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NoteController extends ActionSupport {

	public static final long serialVersionUID = 1L;

	public Notes note = new Notes();
	public List<Object> list = new ArrayList<Object>();
	int userId;
	public Notes getNote() {
		return note;
	}


	public void setNote(Notes note) {
		this.note = note;
	}


	public List<Object> getList() {
		return list;
	}


	public void setList(List<Object> list) {
		this.list = list;
	}


	public int getUserId() {
		return userId;
	}


	public void setUserId(int userId) {
		this.userId = userId;
	}


	public int getNoteId() {
		return noteId;
	}


	public void setNoteId(int noteId) {
		this.noteId = noteId;
	}

	int noteId;
	
	public String create() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		userId = Integer.parseInt(session.get("userId").toString());
		note.setUserId(userId);
		note.setNoteType(1);
		note.setCreated(new Date());
		HibernateUtil.insert(note);
		list = HibernateUtil.list("from Notes as p where p.userId = " + userId + " ORDER BY ID DESC");
		return SUCCESS;
	}
	
	
	public String execute() {
		if (userId == 0)
		{
			Map<String, Object> session = ActionContext.getContext().getSession();
			userId = Integer.parseInt(session.get("userId").toString());
			
		}
		list = HibernateUtil.list("from Notes as p where p.userId = " + userId + " ORDER BY ID DESC");
		return SUCCESS;
	}
	
	public String show() {
		note = HibernateUtil.get(Notes.class, noteId);
		return SUCCESS;
	}
	
}
